package com.visikard.asynctask;

import android.os.Handler;
import android.util.Log;

import com.visikard.application.Constant;
import com.visikard.application.MyApplication;
import com.visikard.application.UIMessage;
import com.visikard.business.AccountBUS;
import com.visikard.item.VKregisterSession;
import com.visikard.response.BaseResponse;
import com.visikard.response.GetProfileResponse;
import com.visikard.response.LoginResponse;

public class LoginTask extends BaseTask {
	public static final int POS_USERNAME = 0;
	public static final int POS_PASSWORD = 1;

	public LoginTask(Handler handler) {
		super(handler);
	}
	
	public LoginTask(Handler handler, boolean isShowProgress) {
		super(handler, isShowProgress);
	}

	/* (non-Javadoc)
	 * @see android.os.AsyncTask#doInBackground(java.lang.Object[])
	 */
	@Override
	protected Object doInBackground(Object... params) {
		String username = (String)params[POS_USERNAME];
		String password = (String)params[POS_PASSWORD];
		
		//validation
		if (username == null || password == null || username.isEmpty() || password.isEmpty()) {				
			mUIHandler.sendEmptyMessage(UIMessage.MSG_LOGIN_INCOMPLETE);
			return null;
		}
		
		Constant.username = username;
		Constant.password = password;
		AccountBUS busObj = new AccountBUS();
		LoginResponse response = busObj.loginApp(username, password);
		
		if(response != null && response.getErrorCode() == BaseResponse.SUCCESS){
			Constant.account = response.getAccount();
			Constant.uid = response.getUid();				
			Constant.loginSession = response.getLoginSession();
			Constant.merchantLogo = response.getMerchantLogo();
			Constant.merchantName = response.getMerchantName();				
			Log.i("NghiDo", "UserID: " + Constant.uid);		
			
			//parse user default
			GetProfileResponse getProfileResponse = busObj.parseProfileDefault();
			if(getProfileResponse.getErrorCode() == BaseResponse.SUCCESS){
				Constant.defaultProfile = getProfileResponse.getUserProfile();
				Constant.idKard = Constant.defaultProfile.getIdKards();
				Constant.userVisible = getProfileResponse.getUserVisible();
			}else{
				Log.e(Constant.LOG_TAG, "LoginTask: parseProfileDefault " + getProfileResponse.getErrorMsg());
			}
		}
		
		return response;
	}

	@Override
	protected boolean handleResult(Object result) {
		LoginResponse response = (LoginResponse)result;
		if(response != null){
			switch(response.getErrorCode()){
			case BaseResponse.SUCCESS:		
				VKregisterSession vksession = new VKregisterSession();
				vksession.setUsername(Constant.username);
				vksession.setPassword(Constant.password);
				vksession.setUserId(String.valueOf(Constant.uid));
				vksession.setLoginSession(Constant.loginSession);
				vksession.setKardId(String.valueOf(Constant.idKard));
				vksession.setMerchantLogo(Constant.merchantLogo);
				vksession.setMerchantName(Constant.merchantName);
				MyApplication.instance.saveSession(vksession);
				mUIHandler.sendEmptyMessage(UIMessage.MSG_LOGIN_SUCCESS);			
				return true;
			case LoginResponse.ERROR_WRONG_USERNAME_PASSWORD:
				mUIHandler.sendEmptyMessage(UIMessage.MSG_WRONG_USERNAME_PASSWORD);			
				return true;
			}
		}		
		return false;
	}

}
